package com.zb.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zb.Service.NoteService;
import com.zb.Service.PowerService;
import com.zb.Service.UserService;
import com.zb.entity.Note;
import com.zb.entity.Power;
import com.zb.entity.User;
import com.zb.util.PageUtil;

@Scope("prototype")
@Component
@RequestMapping("/note")
public class NoteController {
	@Resource
	private NoteService noteService;
	@Resource
	private UserService userService;

	@Autowired
	private PowerService ps;
	@RequestMapping(params = "op=show")
	@ResponseBody
	public Map<String, Object> toshow(Integer page, Integer rows,int id,HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		User suser = (User) session.getAttribute("sys_user");
		Power p = ps.getPowerByRoleId(suser.getRole().getId());
		if(p.getShownote()==0){
			return null;
		}
		
		PageUtil<Note> pageUtil = new PageUtil<Note>();
		pageUtil.setIndex(page);
		pageUtil.setSize(rows);
		int count = noteService.getcount(id);
		pageUtil.setCount(count);
		List<Note> nList = noteService.getNoteById(pageUtil.getIndex(), pageUtil.getSize(), id);
		pageUtil.setData(nList);
		map.put("total", pageUtil.getCount());
		map.put("rows", pageUtil.getData());
		return map;
	}
	//删除
	@RequestMapping(params = "op=delect")
	
	public String delectNote(int id,ModelMap modelMap){
		noteService.delectNote(id);
		modelMap.put("msg", "操作成功！");
		return "bq";
	}
	//修改
	@RequestMapping(params = "op=update")
	public String updateNote(int id,String tital,String content,ModelMap modelMap){
		Note note = new Note();
		note.setId(id);
		note.setTital(tital);
		note.setContent(content);
		noteService.updateNote(note);
		modelMap.put("msg", "操作成功！");
		return "bq";
	}
	//添加
	@RequestMapping(params ="op=add")
	public String addNote(int id,String tital,String content,ModelMap modelMap){
		Note note = new Note();
		note.setTital(tital);
		note.setContent(content);
		note.setUser(userService.getUserById(id));
		noteService.saveNote(note);
		modelMap.put("msg", "操作成功！");
		return "bq";
	}
}


